<?php

namespace plugin\agent\app\admin\controller;

use app\common\controller\BaseAdminController;
use plugin\agent\app\admin\logic\AgentUserLogic;
use think\db\Query;

class AgentUserController extends BaseAdminController
{
    protected $idKey = 'agent_id';

    protected function init()
    {
        parent::init();
        $this->logic = new AgentUserLogic();
    }

    public function list()
    {
        $where = [];
        $this->form['user_id'] && $where[] = ['user_id', '=', $this->form['user_id']];
        $query = $this->logic->search($where);
        $query->order("agent_id desc");
        $query->with([
            'level' => function (Query $query) {
                $query->field([
                    'id',
                    'name',
                ]);
            },
            'user' => function (Query $query) {
                $query->field([
                    'id',
                    'nickname',
                    'mobile',
                    'avatar',
                ]);
            },
        ]);
        $data = $this->logic->getPageList($query);
//        $sum = $query->field([
//            'count(1) as count',
//            'sum(agent_order_num) as agent_order_num',
//            'sum(agent_order_amount) as agent_order_amount',
//            'sum(agent_pay_amount) as agent_pay_amount',
//            'sum(total_bonus) as total_bonus',
//            'sum(self_buy_num) as self_buy_num',
//            'sum(self_buy_amount) as self_buy_amount',
//            'sum(self_buy_pay_amount) as self_buy_pay_amount',
//            'sum(draw_amount) as draw_amount',
//            'sum(draw_num) as draw_num',
//            'sum(residue_amount) as residue_amount',
//        ])->find();
//        $data['sum'] = $sum ?? [];
        return $this->success($data);
    }

    /**
     * 指定分销
     */
    public function addByUser()
    {
        $this->logic->addByUser(input());
        return $this->success();
    }
}
